using AutoMapper;
using CSRedis;
using SH.Common.CommonReturn;
using SH.Common.DataDictionary;
using SH.Room.Application.IServices.IRoomNum;
using SH.Room.Domain.RoomClass;
using SH.Room.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace SH.Room.Application.Services
{
    public class RoomNumServices : BaseServices<RoomNum>, IRoomNumServices
    {
        private readonly IRoomNumRepository _roomNumRepository;

        private readonly IRoomTypeRepository _roomTypeRepository;


        private readonly IMapper _mapper;
        public RoomNumServices(IRoomNumRepository roomNumRepository, IMapper mapper, IRoomTypeRepository roomTypeRepository) : base(roomNumRepository)
        {
            _roomNumRepository = roomNumRepository;
            _mapper = mapper;
            _roomTypeRepository = roomTypeRepository;
        }

        /// <summary>
        /// 批量删除房号
        /// </summary>
        /// <param name="roomNum">房号实体</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> BatchDelRoomNum(List<RoomNum> roomNum)
        {
            ApiResult<int> apiResult= new ApiResult<int>();
            var item=_roomNumRepository.BatchDelete(roomNum);
            apiResult.Success(item);
            return apiResult;
        }

        /// <summary>
        /// 房号删除
        /// </summary>
        /// <param name="roomId">房号ID</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> DeleteRoomNumber(int roomId)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            //var type = _roomTypeRepository.GetList().FirstOrDefault();
            var num = _roomNumRepository.DeleteRoomNumber(roomId);
            //type.RoomCount= type.RoomCount - 1;
            //_roomTypeRepository.Upd(type);
            apiResult.Success();
            return apiResult;
        }

        /// <summary>
        /// 字典列表
        /// </summary>
        /// <returns>受影响行数</returns>
        public List<Dic> GetDicList(string Identification)
        {
            var info=_roomNumRepository.GetDics();
           return info;
        }

        /// <summary>
        /// 房号反填
        /// </summary>
        /// <param name="roomId">房号ID</param>
        /// <returns>受影响行数</returns>
        public RoomNum GetFindRoomNum(int roomId)
        {
            var item = _roomNumRepository.GetModelByCondition(x=>x.RoomId==roomId);
            return item;
        }

        /// <summary>
        /// 房号显示查询分页
        /// </summary>
        /// <param name="state">状态查询</param>
        /// <param name="rtname">房型名称</param>
        /// <param name="roomNumber">房号名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<RoomNumListDto>> GetRoomNumList(int rtid, string? roomNumber, int pageIndex, int pageSize)
        {
            //var result = _redis.Get<List<RoomNumListDto>>("RoomNumList326"); 
            var type = _roomTypeRepository.All;
            var num = _roomNumRepository.All;
            var list = num.LeftJoin<RoomType>((a, b) => a.Rtid == b.Rtid).Where((a, b) => rtid == -1 || a.Rtid == rtid).Where((a, b) => string.IsNullOrEmpty(roomNumber) || a.RoomNumber == roomNumber).OrderByDescending((a, b) => a.Rank).Select((a, b) => new RoomNumListDto
            {
                RoomId = a.RoomId,
                Rtid=a.Rtid,
                Rtname=b.Rtname,
                RoomNumber = a.RoomNumber,
                Rank = a.Rank,
                Remark = a.Remark,
                State = a.State,
            });
                int totalCount = 0;
                int pageCount = 0;
               var query = list.ToPageList(pageIndex, pageSize, ref totalCount, ref pageCount);
            //var random = new Random();
            //var randomNumber = random.Next(3, 5);
            //_redis.Set("RoomNumList326", result, randomNumber);
            return new ApiPaging<List<RoomNumListDto>> { Data = query, TotalCount = totalCount, PageCount = pageCount };

        }


        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="roomNumDto">房号添加DTO</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> RoomNumAdd(RoomNumDto roomNumDto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var type = _roomTypeRepository.GetModelByCondition(x => x.Rtid == roomNumDto.Rtid);

            var item = _roomNumRepository.GetListByCondition(x => x.RoomNumber == roomNumDto.RoomNumber).FirstOrDefault();
            if (item == null)
            {
                var roomNum = _mapper.Map<RoomNum>(roomNumDto);
                 _roomNumRepository.Insert(roomNum);

                // 修改房型数量
                type.RoomCount += 1;
                _roomTypeRepository.Upd(type);
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "房号已有，请重新添加");
            }
            
            return apiResult;
        }

        /// <summary>
        /// 房号修改
        /// </summary>
        /// <param name="roomNumUpdDto">房号修改DTO</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> UpdRoomNum(RoomNumUpdDto roomNumUpdDto)
        {
            ApiResult<int> apiResult= new ApiResult<int>();
            var item = _mapper.Map<RoomNum>(roomNumUpdDto);
            var info = _roomNumRepository.Upd(item);
            apiResult.Success(info);
            return apiResult;
        }

        /// <summary>
        /// 房号状态修改
        /// </summary>
        /// <param name="roomid">房号ID</param>
        /// <param name="state">房号状态</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> UpdState(int roomid, bool state)
        {
            ApiResult<int> apiResult= new ApiResult<int>();
            var item = _roomNumRepository.GetModelByCondition(x => x.RoomId == roomid);
            if (item.State == false)
            {
                item.State = true;
            }
            else
            {
                item.State= false;
            }
            var info = _roomNumRepository.Upd(item);
            apiResult.Success();
            return apiResult;
        }
    }
}
